import { getSongLyric } from '../../api/lyric'

export default class Song {
  constructor ({id, mid, singer, name, album, duration, image, url}) {
    this.id = id
    this.mid = mid
    this.singer = singer
    this.name = name
    this.album = album
    this.duration = duration
    this.image = image
    this.url = url
  }

  getSongLyric () {
    getSongLyric(this.id, (res) => {
      console.log(res)
    })
  }
}

export function createSong (musicData) {
  return new Song({
    id: musicData.id,
    mid: musicData.mid,
    singer: filterSinger(musicData.singer),
    name: musicData.name,
    album: musicData.album.name,
    duration: musicData.interval,
    image: `https://y.gtimg.cn/music/photo_new/T002R300x300M000${musicData.album.mid}.jpg`,
    url: musicData.url
  })
}

export function filterSinger (singer) {
  let ret = []
  if (!singer) {
    return ''
  }
  singer.forEach((s) => {
    ret.push(s.name)
  })
  return ret.join('/')
}

export function createRankSongs (musicList) {
  return new Song({
    id: musicList.album.id,
    mid: musicList.mid,
    singer: musicList.singer[0].name,
    name: musicList.name,
    album: musicList.album.name,
    duration: '',
    image: `https://y.gtimg.cn/music/photo_new/T002R300x300M000${musicList.album.mid}.jpg`,
    url: musicList.url
  })
  // return musicList
}